package parser;

import antlr.RecognitionException;
import java.util.*;

class LocalVariable extends Expr {
    /** There are three possible states for a variable:      Load           Store
     * Fast local: bound in the function,                    LOAD_FRAME     STORE_FRAME         
     * Local: Bound in a class def or some other context.    LOAD_LOCAL     STORE_LOCAL
     * Enclosed: bound in an enclosing function.             LOAD_DEREF     STORE_DEREF
     * Global: Not bound in a function .                     LOAD_GLOBAL    STORE_GLOBAL 
     *  LOAD_FRAME  index -- Offset into locals array
     *  LOAD_LOCAL    name --- Name of variable
     *  LOAD_DEREF    index depth --- Offset into local array and static link count
     *  LOAD_GLOBAL   name --- Name of variable
     */                 
    
    int name;
    LocalScope scope;
    Type type;
    
    LocalVariable(int name, LocalScope scope) {
        this.name = name;
        this.scope = scope;
    }

    void write(ByteStream out) throws RecognitionException {
        if (type == null)
            type = scope.variable(name);
        type.write(out);
    }       

    void delete(ByteStream out) throws RecognitionException {
        if (type == null)
            type = scope.variable(name);
        type.delete(out);
    }
    
    void writeLHS(ByteStream out) throws RecognitionException {
        if (type == null)
            type = scope.variable(name);
        type.writeLHS(out);
    }

    void lhs() {
        scope.bind(name);
    }
    
}

